package com.yxq.mdt.app.modules.spikeorder.model;

import com.alibaba.fastjson.JSON;
import com.orhanobut.hawk.Hawk;
import com.yxq.mdt.app.base.RespBase;
import com.yxq.mdt.app.base.RespBasePay;
import com.yxq.mdt.app.base.ibase.IBaseModel;
import com.yxq.mdt.app.constant.Constant;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.login.bean.UserBean;
import com.yxq.mdt.app.modules.order.bean.OrderConfirmBean;
import com.yxq.mdt.app.modules.order.bean.OrderPayResultWxBean;
import com.yxq.mdt.app.modules.order.bean.UploadImageResultBean;
import com.yxq.mdt.app.modules.order.model.OrderApi;
import com.yxq.mdt.app.modules.spikeorder.bean.ExpressBean;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderCommentPreviewBean;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderDetailBean;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderListBean;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderPayResultBalanceBean;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderRefundDetailBean;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderRefundPreviewBean;
import com.yxq.mdt.app.modules.spikeorder.bean.OrderSubmitResult;
import com.yxq.mdt.app.modules.spikeorder.bean.SpikeOrderConfirmBean;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.net.BaseObserver;
import com.yxq.mdt.app.net.RetrofitFactory;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class SpikeOrderModel implements IBaseModel {

    public void preview(String address_id, String mch_list, ApiModel.CommonCallback<SpikeOrderConfirmBean> callback) {
        UserBean userBean = Hawk.get(SPKey.SP_KEY_USER_INFO);
        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .preview(userBean != null ? userBean.getAccess_token() : "", address_id, mch_list)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<SpikeOrderConfirmBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<SpikeOrderConfirmBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void list(String status, int page, ApiModel.CommonCallback<OrderListBean> callback) {
        UserBean userBean = Hawk.get(SPKey.SP_KEY_USER_INFO);
        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .list(userBean != null ? userBean.getAccess_token() : "", status, page)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<OrderListBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<OrderListBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void submit(String address_id, String mch_list, int use_integral, int payment,String remark, ApiModel.CommonCallback<OrderSubmitResult> callback) {
        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .submit(Constant.getToken(), address_id, mch_list, use_integral, payment,remark)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<OrderSubmitResult>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<OrderSubmitResult> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void payBalance(String order_id, String parent_user_id, String condition, ApiModel.CommonCallback<OrderPayResultBalanceBean> callback) {
        UserBean userBean = Hawk.get(SPKey.SP_KEY_USER_INFO);
        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .payBalance(userBean != null ? userBean.getAccess_token() : "", order_id, "BALANCE_PAY", "wx")
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<OrderPayResultBalanceBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<OrderPayResultBalanceBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void payWX(String order_id, String parent_user_id, String condition, ApiModel.CommonCallback<OrderPayResultWxBean> callback) {
        UserBean userBean = Hawk.get(SPKey.SP_KEY_USER_INFO);
        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .payWX(userBean != null ? userBean.getAccess_token() : "", order_id, "WECHAT_PAY", parent_user_id, condition, "wx")
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBasePay<OrderPayResultWxBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBasePay<OrderPayResultWxBean> respBase) {
                        if (respBase.getCode() == 0) {
                            OrderPayResultWxBean orderPayResultWxBean = respBase.getData();
                            orderPayResultWxBean.setBody(respBase.getBody());
                            OrderPayResultWxBean.ResBean resBean = JSON.parseObject(respBase.getRes(), OrderPayResultWxBean.ResBean.class);
                            orderPayResultWxBean.setRes(resBean);
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void detail(String order_id, ApiModel.CommonCallback<OrderDetailBean> callback) {
        UserBean userBean = Hawk.get(SPKey.SP_KEY_USER_INFO);
        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .detail(userBean != null ? userBean.getAccess_token() : "", order_id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<OrderDetailBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<OrderDetailBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void express(String order_id, String type, ApiModel.CommonCallback<ExpressBean> callback) {
        UserBean userBean = Hawk.get(SPKey.SP_KEY_USER_INFO);
        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .express(userBean != null ? userBean.getAccess_token() : "", order_id, type)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<ExpressBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<ExpressBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    /**
     * 退款
     *
     * @param order_id
     * @param callback
     */
    public static void revoke(String order_id, ApiModel.CommonCallback<String> callback) {
        UserBean userBean = Hawk.get(SPKey.SP_KEY_USER_INFO);
        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .revoke(userBean != null ? userBean.getAccess_token() : "", order_id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getMsg());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    /**
     * 确认收货
     *
     * @param order_id
     * @param callback
     */
    public static void confirm(String order_id, ApiModel.CommonCallback<String> callback) {
        UserBean userBean = Hawk.get(SPKey.SP_KEY_USER_INFO);
        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .confirm(userBean != null ? userBean.getAccess_token() : "", order_id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getMsg());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    /**
     * 申请售后预览界面
     *
     * @param order_id
     * @param callback
     */
    public static void refundPreview(String order_id, ApiModel.CommonCallback<OrderRefundPreviewBean> callback) {

        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .refundPreview(Constant.getToken(), order_id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<OrderRefundPreviewBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<OrderRefundPreviewBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    /**
     * 提交退货换货
     *
     * @param order_id  订单id
     * @param type      类型（1退货 2换货）
     * @param desc      退换货描述
     * @param picList   上传图片列表url集合
     * @param orderType 订单类型 STORE
     * @param callback
     */
    public static void refund(String order_id, int type, String desc, String picList, String orderType, ApiModel.CommonCallback<String> callback) {

        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .refund(Constant.getToken(), type, order_id, desc, picList, orderType)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getMsg());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    /**
     * 上传图片
     *
     * @param body
     * @param callback
     */
    public static void uploadImage(RequestBody body, ApiModel.CommonCallback<UploadImageResultBean> callback) {

        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .uploadImage(body)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<UploadImageResultBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<UploadImageResultBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    /**
     * 评论预览界面
     *
     * @param order_id
     * @param callback
     */
    public static void commentPreview(String order_id, String type, ApiModel.CommonCallback<OrderCommentPreviewBean> callback) {

        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .commentPreview(Constant.getToken(), order_id, type)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<OrderCommentPreviewBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<OrderCommentPreviewBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    /**
     * 提交评论
     *
     * @param order_id   订单id
     * @param type
     * @param goods_list
     * @param callback
     */
    public static void orderComment(String order_id, String type, String goods_list, ApiModel.CommonCallback<String> callback) {

        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .orderComment(Constant.getToken(), order_id, type, goods_list)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getMsg());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    /**
     * 订单-售后订单详情
     *
     * @param order_refund_id 订单id
     * @param callback
     */
    public static void refundDetail(String order_refund_id, ApiModel.CommonCallback<OrderRefundDetailBean> callback) {

        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .refundDetail(Constant.getToken(), order_refund_id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<OrderRefundDetailBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<OrderRefundDetailBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    /**
     * 提交退货物流
     *
     * @param callback
     */
    public static void refundSend(String order_refund_id, String express, String express_no, String orderType, ApiModel.CommonCallback<String> callback) {
        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .refundSend(Constant.getToken(), order_refund_id, express, express_no, orderType)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getMsg());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }
    public static void delete(String order_id, ApiModel.CommonCallback<String> callback) {
        UserBean userBean = Hawk.get(SPKey.SP_KEY_USER_INFO);
        RetrofitFactory.getRetrofit().create(SpikeOrderApi.class)
                .delete(userBean != null ? userBean.getAccess_token() : "", order_id)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<String>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<String> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }
    @Override
    public void onDestroy() {

    }
}
